package nl.boelen.game.entities.functions;

import nl.boelen.game.client.Client;
import nl.boelen.game.entities.Player;
import nl.boelen.game.gfx.Colors;
import nl.boelen.game.net.packet.Packet30Health;

public class Health {
    private float hp;
    private float maxhp;
    private boolean isDeath = false;
    private boolean isClient;
    private Client client;
    private Player player;

    public Health(int hp, boolean isClient, Client client, Player player) {
        this.hp = hp;
        this.maxhp = hp;
        this.client = client;
        this.player = player;
        this.isClient = isClient;
    }

    public void percentHit(int percent) {
        float p = (float) percent / 100;
        hp -= (maxhp * p);
        if (hp < 0f) {
            hp = 0;
            isDeath = true;
        } else {
            sendhealthPacket();
        }
    }

    public void valueHit(int value) {
        hp -= value;
        if (hp < 0f) {
            hp = 0;
            isDeath = true;
        } else {
            sendhealthPacket();
        }
    }

    public void addPercent(int percent) {
        float p = (float) percent / 100;
        hp += (maxhp * p);
        if (hp > maxhp) {
            hp = maxhp;
        } else {
            sendhealthPacket();
        }
    }

    public void addValue(int value) {
        hp += value;
        if (hp > maxhp) {
            hp = maxhp;
        } else {
            sendhealthPacket();
        }
    }

    private void sendhealthPacket() {
        client.TCPSender.sendPacket(new Packet30Health(player.getUniqueKey(), maxhp, hp, isDeath));
    }

    public int getHP() {
        return (int) hp;
    }

    public int getMaxHP() {
        return (int) maxhp;
    }

    public boolean isDeath() {
        return isDeath;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void render(int xHealth, int yHealth) {
        if (!isClient) {
            renderHealth(xHealth, yHealth, 10 * player.getScale());
        } else {
            renderHealthBarClient(0, client.screen.height - 1, client.screen.width, 2);
        }
    }

    public void renderHealth(int xPos, int yPos, int totalWidth) {
        xPos -= client.screen.xOffset;
        yPos -= client.screen.yOffset;

        int percent = getHP() / (getMaxHP() / 100);
        int healthPixels = (totalWidth * percent) / 100;

        for (int w = 0; w < totalWidth; w++) {
            if (xPos >= 0 && xPos < client.screen.width && yPos >= 0 && yPos < client.screen.height) {
                if (w < healthPixels) {
                    client.screen.pixels[xPos + yPos * client.screen.width] = Colors.get(151);
                } else {
                    client.screen.pixels[xPos + yPos * client.screen.width] = Colors.get(500);
                }
            }
            xPos++;
        }
    }

    public void renderHealthBarClient(int xPos, int yPos, int totalWidth, int totalHeight) {
        int percent = getHP() / (getMaxHP() / 100);
        int healthPixels = (totalWidth * percent) / 100;

        for (int w = 0; w < totalWidth; w++) {
            if (xPos >= 0 && xPos < client.screen.width && yPos >= 0 && yPos < client.screen.height) {
                for (int h = 0; h < totalHeight; h++) {
                    if (w < healthPixels) {
                        client.screen.pixels[xPos + (yPos - h) * client.screen.width] = Colors.get(40);
                    } else {
                        client.screen.pixels[xPos + (yPos - h) * client.screen.width] = Colors.get(500);
                    }
                }
            }
            xPos++;
        }
        String msg = "" + getHP();
        client.font.render(msg, client.screen.xOffset + 82 - (msg.length() * 8), yPos += client.screen.yOffset - 7,
                Colors.get(-1, -1, -1, 555), 1);
    }
}
